package com.oss.aop;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.oss.vo.BaseEntity;
@Aspect    
@Component 
public class AopService {

	@Pointcut(value="@annotation(com.oss.annotation.PreUpdateTime)")
	public void aspect(){}
	
	@Before("aspect()")  
    public void doAccessCheck(JoinPoint point){  
		Calendar car = Calendar.getInstance();
		car.setTime(new Date());
		
		for(Object entity:point.getArgs()){
			
			if(entity instanceof Collection){
				Collection<BaseEntity> c = (Collection<BaseEntity>) entity;
				
				for(BaseEntity e:c){
					if(e.getCrtDate() == null){
						e.setCrtDate(car.getTime());
						e.setIsValid(1);
					}
					e.setOptDate(car.getTime());
				}
			}else{
				BaseEntity e =(BaseEntity) entity;
				if(e.getCrtDate() == null){
					e.setCrtDate(car.getTime());
					e.setIsValid(1);
				}
				e.setOptDate(car.getTime());
			}
		}
    }
}
